<template>
  <el-dialog
    title="微信发货信息管理配置"
    :visible.sync="dialogVisible"
    width="600px"
    :before-close="handleClose"
  >
    <el-form ref="configForm" :model="configForm" label-width="140px" :rules="rules">
      <el-form-item label="启用状态：" prop="enabled">
        <el-switch
          v-model="configForm.enabled"
          active-text="启用"
          inactive-text="禁用"
        />
        <div class="form-tip">启用后将自动上传发货信息到微信小程序平台</div>
      </el-form-item>

      <el-form-item label="自动上传：" prop="autoUpload">
        <el-switch
          v-model="configForm.autoUpload"
          active-text="开启"
          inactive-text="关闭"
        />
        <div class="form-tip">开启后发货时自动上传，关闭后需要手动上传</div>
      </el-form-item>

      <el-form-item label="特殊商品处理：" prop="specialMode">
        <el-radio-group v-model="configForm.specialMode">
          <el-radio :label="1">正常发货</el-radio>
          <el-radio :label="2">特殊发货报备</el-radio>
        </el-radio-group>
        <div class="form-tip">特殊商品（如虚拟商品）的发货处理方式</div>
      </el-form-item>

      <el-form-item label="失败重试次数：" prop="retryTimes">
        <el-input-number
          v-model="configForm.retryTimes"
          :min="0"
          :max="10"
          controls-position="right"
        />
        <div class="form-tip">上传失败时的重试次数</div>
      </el-form-item>

      <el-form-item label="重试间隔（秒）：" prop="retryInterval">
        <el-input-number
          v-model="configForm.retryInterval"
          :min="10"
          :max="300"
          controls-position="right"
        />
        <div class="form-tip">重试间隔时间</div>
      </el-form-item>
    </el-form>

    <div slot="footer">
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="submitConfig" :loading="loading">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getWechatShippingConfigApi, updateWechatShippingConfigApi } from '@/api/wechatShipping'

export default {
  name: 'WechatShippingConfig',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogVisible: false,
      loading: false,
      configForm: {
        enabled: false,
        autoUpload: true,
        specialMode: 1,
        retryTimes: 3,
        retryInterval: 30
      },
      rules: {
        retryTimes: [
          { required: true, message: '请输入重试次数', trigger: 'blur' }
        ],
        retryInterval: [
          { required: true, message: '请输入重试间隔', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    visible(val) {
      this.dialogVisible = val
      if (val) {
        this.loadConfig()
      }
    },
    dialogVisible(val) {
      this.$emit('update:visible', val)
    }
  },
  methods: {
    async loadConfig() {
      try {
        const res = await getWechatShippingConfigApi()
        this.configForm = { ...res.data }
      } catch (error) {
        this.$message.error('加载配置失败: ' + error.message)
      }
    },

    async submitConfig() {
      this.$refs.configForm.validate(async (valid) => {
        if (valid) {
          this.loading = true
          try {
            await updateWechatShippingConfigApi(this.configForm)
            this.$message.success('配置保存成功')
            this.$emit('refresh')
            this.handleClose()
          } catch (error) {
            this.$message.error('保存失败: ' + error.message)
          } finally {
            this.loading = false
          }
        }
      })
    },

    handleClose() {
      this.dialogVisible = false
      this.$refs.configForm.resetFields()
    }
  }
}
</script>

<style scoped lang="scss">
.form-tip {
  font-size: 12px;
  color: #909399;
  margin-top: 5px;
}
</style> 